package com.ctvit.sjk.knowledge.app.controller;

import com.ctvit.sjk.knowledge.app.pojo.dto.AppChatMessageDTO;
import com.ctvit.sjk.knowledge.app.service.ITabAppChatRecordService;
import com.ctvit.sjk.knowledge.utils.SseServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * 应用聊天Controller
 *
 * @author heyingcheng
 * @date 2025/03/30
 */
@Slf4j
@RestController
@RequestMapping("knowledge/application")
public class TabAppChatController {

    private static final String CREATE_ID = "1";

    private final ITabAppChatRecordService tabAppChatRecordService;

    public TabAppChatController(ITabAppChatRecordService tabAppChatRecordService) {
        this.tabAppChatRecordService = tabAppChatRecordService;
    }

    /**
     * 开启应用聊天
     */
    @PostMapping(value = "chat_message/{chatId}", produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
    public Object message(@PathVariable String chatId, @RequestBody AppChatMessageDTO dto) {
        SseEmitter connect = SseServer.connect(CREATE_ID);
        new Thread(() -> tabAppChatRecordService.message(chatId, dto, CREATE_ID)).start();
        return connect;
    }

}
